<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\admin\logic\article;

use app\admin\model\article\ArticleCategory;
use app\basic\BaseLogic;
use exception\ApiException;
use support\utils\Helper;
use app\admin\model\article\Article;

/**
 * 文档管理逻辑层
 */
class ArticleLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new Article();
    }

    /**
     * 获取分类下的推荐文章
     * @param int $categoryId 分类ID
     * @param int $limit 获取数量
     * @return array
     */
    public function getRecommendedArticles($categoryId, $limit = 6,$page=1)
    {
        $query = $this->where([
            'status' => 1,
        ])->whereIn('category_id', $categoryId)
        ->field("id,title,describe,content,create_time,update_time,status,category_id,fileext,release_time,file_type,is_vip")
        ->order("create_time", "desc");

         // 使用分页查询
        $result = $query->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);

        // 返回格式化数据
        return [
            'list' => $result->items(),
            'total' => $result->total(),
            'page' => $result->currentPage(),
            'limit' => $limit
        ];
    }

    /**
     * 获取文章列表
     * @param int $categoryId 分类ID
     * @param int $page 页码
     * @param int $limit 每页数量
     * @return array
     */
    public function getArticleList(int $categoryId = 0, int $page = 1, int $limit = 10,$keyword=''): array
    {
        // 构建查询条件
        $where = [];
        $category_id = [];
        $where['status'] = 1; // 只查询启用状态的文档
        if ($categoryId > 0) {
            // 获取当前分类是否是一级分类
            $parent_id = (new ArticleCategory())->where(['id' => $categoryId])->value("parent_id");
            if(!$parent_id){
                $category_id = (new ArticleCategory())->where(['parent_id' => $categoryId])->column('id');
            }else{
                $where['category_id'] = $categoryId;
            }
        }

        // 查询文档列表和总数
        $query = $this->where($where)
            ->field("id,title,describe,content,create_time,update_time,status,category_id,fileext,release_time,file_type,is_vip")
            ->order("create_time", "desc");
           // 增加关键词搜索
        if(!empty($keyword)){
            $query->where("title|describe",'like','%'.$keyword.'%');
        }
        if(!empty($category_id)){
            $query->whereIn("category_id",$category_id);
        }
        // 使用分页查询
        $result = $query->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);

        // 返回格式化数据
        return [
            'list' => $result->items(),
            'total' => $result->total(),
            'page' => $result->currentPage(),
            'limit' => $limit
        ];
    }
}
